#!/usr/bin/perl

use strict;
use Test::More tests => 42;
use FindBin qw($Bin);
use lib "$Bin/lib";
use MemcachedTest;

my $server = new_memcached();
my $sock = $server->sock;

# set foo (and should get it)
print $sock "set foo 0 0 6\r\nfooval\r\n";
is(scalar <$sock>, "STORED\r\n", "stored foo");
mem_get_or_lock_is($sock, "foo", "fooval");

# delete foo.
print $sock "delete foo\r\n";
is(scalar <$sock>, "DELETED\r\n", "deleted foo");

# get should has no value
mem_get_is($sock, "foo", undef);

# get_or_lock should has no value
mem_get_or_lock_is($sock, "foo", undef);
mem_get_or_lock_is($sock, "foo", undef);

# get_or_lock should has value
mem_get_or_lock_is($sock, "foo", "fooval");

# set foo (and should get it)
print $sock "set foo 0 2 6\r\nfoobar\r\n";
is(scalar <$sock>, "STORED\r\n", "stored foo");
mem_get_or_lock_is($sock, "foo", "foobar");

# get should has value
mem_get_is($sock, "foo", "foobar");

sleep(3);
mem_get_is($sock, "foo", undef);

# get_or_lock should has value
mem_get_or_lock_is($sock, "foo", undef);
mem_get_or_lock_is($sock, "foo", undef);
mem_get_or_lock_is($sock, "foo", "foobar");

# get_or_lock should has value
sleep(1);
mem_get_or_lock_is($sock, "foo", undef);
mem_get_or_lock_is($sock, "foo", undef);
mem_get_or_lock_is($sock, "foo", "foobar");

# get_or_lock should has no value
sleep(3);
mem_get_or_lock_is($sock, "foo", undef);
mem_get_or_lock_is($sock, "foo", undef);
mem_get_or_lock_is($sock, "foo", undef);
mem_get_is($sock, "foo", undef);

# set foo (and should get it)
print $sock "set foo 0 0 6\r\nfooval\r\n";
is(scalar <$sock>, "STORED\r\n", "stored foo");
mem_get_or_lock_is($sock, "foo", "fooval");

# get should has value
mem_get_is($sock, "foo", "fooval");

sleep(3);
mem_get_is($sock, "foo", "fooval");

# delete foo.
print $sock "delete foo 2\r\n";
is(scalar <$sock>, "DELETED\r\n", "deleted foo");

mem_get_is($sock, "foo", "fooval");
mem_get_or_lock_is($sock, "foo", "fooval");

sleep(3);
mem_get_is($sock, "foo", undef);

# get_or_lock should has no value
mem_get_or_lock_is($sock, "foo", undef);
mem_get_or_lock_is($sock, "foo", undef);
mem_get_or_lock_is($sock, "foo", "fooval");
mem_get_or_lock_is($sock, "foo", "fooval");

# get_or_lock should has value
mem_get_or_lock_is($sock, "foo", "fooval");

# get_or_lock should has value
sleep(1);
mem_get_or_lock_is($sock, "foo", undef);
mem_get_or_lock_is($sock, "foo", undef);
mem_get_or_lock_is($sock, "foo", "fooval");

# get_or_lock should has no value
sleep(3);
mem_get_or_lock_is($sock, "foo", undef);
mem_get_or_lock_is($sock, "foo", undef);
mem_get_or_lock_is($sock, "foo", undef);
mem_get_or_lock_is($sock, "foo", undef);
mem_get_is($sock, "foo", undef);
